<?php
/**
 * Created by PhpStorm.
 * User: work
 * Date: 18-8-11
 * Time: 下午7:10
 */

namespace PddWechat\Models\Admin\Service;


use Dai\Framework\Base\BaseSessionInfo;
use Dai\Framework\Library\RedisExt;
use PddWechat\Library\PddLibrary;

class GoodsSet
{
    /**
     * @param $basePageInfo
     * @return array
     */
    public function execute($basePageInfo) {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;

        if( $sessionData->adminLevel !== 10) {
            header("/mall/index");
            exit;
        }

        $oldGoodsIds = RedisExt::getInstance()->get("hot_goods_ids");
        $oldGoodsIds = explode(",", $oldGoodsIds);

        $validGoodsIds = [];
        $params = $basePageInfo->params;
        $goodsIds = explode(",", $params->goodsIds);
        foreach($goodsIds as $goodsId) {
            if( intval($goodsId) > 0) {
                $goodsInfo = PddLibrary::getGoodsInfo($goodsId);
                if( $goodsInfo != false) {
                    $validGoodsIds[] = $goodsId;
                    RedisExt::getInstance()->setex("hot_goods_detail_$goodsId",86400 * 90, json_encode($goodsInfo));
                }
            }
        }

        if( count($validGoodsIds) === 0) {
            breakF();
        }

        RedisExt::getInstance()->set("hot_goods_ids", implode(",", $validGoodsIds));

        foreach ($oldGoodsIds as $goodsId) {
            if( ! in_array($goodsId, $validGoodsIds)) {
                RedisExt::getInstance()->del("hot_goods_detail_$goodsId");
            }
        }
        $this->breakF();
    }

    public function breakF(){
        header('Location: /admin/goods/list');
        exit;
    }
}